package com.my.core.util.spring;

import cn.hutool.core.util.StrUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;

/**
 * 处理不能通过依赖注入方式加入的类 的环境获取 --- 详情看okhttp的config配置
 * 使用方式为在服务启动类中进行注入
 * @author timo 2020/11/25
 */
public class SpringUtilStatic {
    private static ApplicationContext applicationContext;

    public static int environ = 0;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext){
        SpringUtilStatic.applicationContext = applicationContext;
        Environment environment = applicationContext.getEnvironment();
        String[] activeProfiles = environment.getActiveProfiles();
        for(String profi:activeProfiles){
            if("dev".equals(profi)){
                break;
            }else if("test".equals(profi)){
                environ = 1;
                break;
            }else if("preview".equals(profi)){
                environ = 2;
                break;
            }else if("pro".equals(profi)){
                environ = 3;
                break;
            }
        }
    }

    /**
     * 获取应用名称
     *
     * @author fengshuonan
     * @Date 2018/5/12 上午10:24
     */
    public static String getPro(String name) {
        try {
            Environment environment = getApplicationContext().getEnvironment();
            String property = environment.getProperty(name);
            if (StrUtil.isNotEmpty(property)) {
                return property;
            } else {
                return "";
            }
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 判断是否是windows操作系统
     *
     * @author stylefeng
     * @Date 2017/5/24 22:34
     */
    public static Boolean isWinOs() {
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取临时目录
     *
     * @author stylefeng
     * @Date 2017/5/24 22:35
     */
    public static String getTempPath() {
        return System.getProperty("java.io.tmpdir");
    }


}
